package class16;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;

public class Code01_BFS {

    // 图宽度优先遍历
    public static void bfs(MyGraph.Node<Integer> node) {
        if (node == null) {
            return;
        }
        Queue<MyGraph.Node<Integer>> queue = new ArrayDeque<>();
        queue.add(node);
        Set<MyGraph.Node<Integer>> set = new HashSet<>();
        set.add(node);
        while (!queue.isEmpty()) {
            node = queue.poll();
            System.out.println(node.value);
            for (MyGraph.Node<Integer> next : node.nexts) {
                if (set.contains(next)) {
                    continue;
                }
                queue.add(next);
                set.add(next);
            }
        }
    }

}
